<?php

	function dateCmp($a, $b) {
		return $b[1] - $a[1];
	}

	function sortByDate(&$files) {
		usort($files, 'dateCmp');
	}

	function nl($str) {
		return $str."\n";
	}

	function microtime_float() {
		list($usec, $sec) = explode(" ", microtime());
		return ((float)$usec + (float)$sec);
	}

	function endsWith($str, $sub) {
		return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
	}


	function filenameSafe($filename) {
		$temp = $filename;

		// Replace spaces with a '_'
		$temp = str_replace(" ", "_", $temp);

		// Loop through string
		$result = '';
		for ($i=0; $i<strlen($temp); $i++) {
			if (preg_match('([0-9]|[a-zA-Z]|_)', $temp[$i])) {
			    $result = $result . $temp[$i];
			}
		}

		// Return filename
		return $result;
	}

	function rm_recursive($filepath) {

	    if (is_dir($filepath) && !is_link($filepath)) {

		if ($dh = opendir($filepath)) {

			while (($sf = readdir($dh)) !== false) {

				if ($sf == '.' || $sf == '..') {
				    continue;
				}

				if (!rm_recursive($filepath.'/'.$sf)) {
				    throw new Exception($filepath.'/'.$sf.' could not be deleted.');
				}
			}

		    	closedir($dh);
		}

		return rmdir($filepath);
	    }

	    return unlink($filepath);
	}

?>